package com.event;

import com.mysqld.Mysqld;
import com.window.LoginStart;
import com.window.Manage;
import com.window.StudentSystem;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginEvent implements ActionListener {
    public static String accountA; //记录登录的账号
    public static String apassword; //记录登录的账号
    public static Manage manage;
    public static String status;

    @Override
    public void actionPerformed(ActionEvent e) {
        //如果账号和密码与数据库中的一致就登录，同时写入状态数据
        try {
            String account = LoginStart.accounttext.getText();//获取输入的账号
            char[] str = LoginStart.passwordtext.getPassword();//获取输入的密码
            String password = new String(str);
            this.accountA = account;
            this.apassword = password;

            //判断账号与密码是否正确
            if (Mysqld.loginAccount(accountA, apassword)) {
                status = Mysqld.selectstatus(); //判断账号的登录状态
                if (status.equals("1")) {
                    int a = JOptionPane.showConfirmDialog(null, "该账号已被其他人登录,是否继续登录!", "登录信息", JOptionPane.YES_NO_OPTION);
                    //a==0代表确认继续登录
                    if (a == 0) {
                        StudentSystem.loginStartA.dispose();//关闭登录界面
                        Manage b = new Manage();//打开管理界面
                        Mysqld.setStatus(1);//登录成功后设置状态为1
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "登录成功!", "登录信息", JOptionPane.WARNING_MESSAGE);
                    StudentSystem.loginStartA.dispose();//关闭登录界面
                    Manage a = new Manage();//打开管理界面
                    Mysqld.setStatus(1);//登录成功后设置状态为1
                    manage = a;
                }
            } else {
                JOptionPane.showMessageDialog(null, "账号或密码错误!", "登录信息", JOptionPane.WARNING_MESSAGE);
            }
        } catch (Exception e1) {
        }
    }
}
